package com.lagou.test;

import com.lagou.dao.IUserDao;
import com.lagou.io.Resources;
import com.lagou.pojo.User;
import com.lagou.sqlSession.SqlSession;
import com.lagou.sqlSession.SqlSessionFactory;
import com.lagou.sqlSession.SqlSessionFactoryBuilder;
import org.dom4j.DocumentException;
import org.junit.Test;

import javax.jws.soap.SOAPBinding;
import java.beans.PropertyVetoException;
import java.io.InputStream;
import java.util.List;

public class IPersistence {

    //测试查询所有用户和查询一个用户
    @Test
    public void testSelect() throws Exception {
        //使用自己写的流读取类进行xml读取
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");

        //根据读取的xml信息创建sqlSession
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //使用代理接口调用
        //代理对象调用接口中任意方法，都是执行invoke方法
        IUserDao userDao = sqlSession.getMapper(IUserDao.class);

        List<User> all = userDao.findAll();
        for (User item : all) {
            System.out.println(item);
        }

        System.out.println("单独调用测试");
        User daoUser = new User();
        daoUser.setId(1);
        daoUser.setUsername("lisi");
        System.out.println(userDao.findByCondition(daoUser));
    }

    //测试新增一个用户
    @Test
    public void testAdd() throws Exception {
        //使用自己写的流读取类进行xml读取
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");

        //根据读取的xml信息创建sqlSession
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //使用代理接口调用
        //代理对象调用接口中任意方法，都是执行invoke方法
        IUserDao userDao = sqlSession.getMapper(IUserDao.class);
        User user = new User();
        user.setId(6);
        user.setUsername("testing_user");
        user.setPassword("1234567");
        user.setBirthday("1998-01-01");
        userDao.addUser(user);
    }

    //测试更新一个用户
    @Test
    public void testUpdate() throws Exception {
        //使用自己写的流读取类进行xml读取
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");

        //根据读取的xml信息创建sqlSession
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //使用代理接口调用
        //代理对象调用接口中任意方法，都是执行invoke方法
        IUserDao userDao = sqlSession.getMapper(IUserDao.class);
        User user = new User();
        user.setId(6);
        user.setUsername("testing_user_update");
        user.setPassword("1234567_update");
        user.setBirthday("1998-01-03");
        userDao.updateUser(user);
    }

    //测试删除一个用户
    @Test
    public void testDelete() throws Exception {
        //使用自己写的流读取类进行xml读取
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");

        //根据读取的xml信息创建sqlSession
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //使用代理接口调用
        //代理对象调用接口中任意方法，都是执行invoke方法
        IUserDao userDao = sqlSession.getMapper(IUserDao.class);

        User user = new User();
        user.setId(6);
        userDao.deleteUser(user);
    }

}
